package com.example.course.controller;

import com.example.course.dto.DirectoryDTO;
import com.example.course.service.DirectoryService;
import com.example.course.tool.R;
import com.example.course.vo.DirectoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 课程目录控制器
 *
 * @author Jasmine-drs
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/api/directory")
@Api(tags = "课程目录接口")
public class DirectoryController {

	@Autowired
	private DirectoryService directoryService;

	@PostMapping
	@ApiOperation("添加课程目录（仅教师）")
	public R<DirectoryVO> addDirectory(
			@ApiParam(value = "目录信息", required = true) @Valid @RequestBody DirectoryDTO directoryDTO,
			@ApiParam(value = "用户角色", required = true) @RequestParam boolean isTeacher) {
		try {
			DirectoryVO directoryVO = directoryService.addDirectory(directoryDTO, isTeacher);
			return R.success(directoryVO);
		} catch (Exception e) {
			return R.err("添加课程目录失败：" + e.getMessage());
		}
	}

	@GetMapping("/course/{courseId}")
	@ApiOperation("根据课程ID获取目录列表")
	public R<List<DirectoryVO>> getDirectoryByCourseId(
			@ApiParam(value = "课程ID", required = true) @PathVariable Integer courseId) {
		try {
			List<DirectoryVO> directoryVOList = directoryService.getDirectoryByCourseId(courseId);
			return R.success(directoryVOList);
		} catch (Exception e) {
			return R.err("获取课程目录失败：" + e.getMessage());
		}
	}

	@GetMapping("/{id}")
	@ApiOperation("根据ID获取目录")
	public R<DirectoryVO> getDirectoryById(
			@ApiParam(value = "目录ID", required = true) @PathVariable Integer id) {
		try {
			DirectoryVO directoryVO = directoryService.getDirectoryById(id);
			return R.success(directoryVO);
		} catch (Exception e) {
			return R.err("获取课程目录失败：" + e.getMessage());
		}
	}
} 